package com.example.question.list.list1;

/**
 * @Date 2023-07-06
 * @Author Jonathan
 */
public class Code15_24 {
    public static void main(String[] args) {
        Code15_24 code = new Code15_24();
        ListNode listNode = List.createList(new int[]{1, 2, 3});
        ListNode node = code.swapPairs(listNode);
        System.out.println(node);
    }

    /**
     * todo
     *
     * @param head
     * @return
     */
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = head.next;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            // 改变方向
            slow.next.next = slow;
            // 奇数个  fast是最后一个
            if (fast != null && fast.next == null) {
                slow.next = fast;
            } else if (fast != null) {
                slow.next = fast.next;
            } else {
                slow.next = null;
            }

            // 改变方向
            slow = fast;
        }
        return newHead;
    }
}
